import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemo {
    public static void test1(Jedis jedis) {
        System.out.println("get 和 set 方法的使用");
        // 先清空数据库
        jedis.flushDB();

        jedis.set("key1","111");
        jedis.set("key2","222");
        SetParams params = new SetParams();
        params.ex(10);
        params.nx();
        jedis.set("key3","333",params);

        String value3 = jedis.get("key3");
        System.out.println("value3 = " + value3);
    }

    public static void test2(Jedis jedis) {
        System.out.println("exists 和 del");
        jedis.flushDB();

        jedis.set("key1","111");
        jedis.set("key2","222");

        System.out.println(jedis.exists("key1"));

        long result = jedis.del("key1","key2");
        System.out.println("result = " + result);
        System.out.println(jedis.exists("key1"));
    }

    public static void test3(Jedis jedis) {
        System.out.println("keys");
        jedis.flushDB();
        jedis.set("key1","111");
        jedis.set("key2","222");
        jedis.set("key3","333");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);
    }

    public static void test4(Jedis jedis) {
        System.out.println("expire 和 ttl");
        jedis.flushDB();

        jedis.set("key1","111");
        jedis.expire("key1",10);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        long time = jedis.ttl("key1");
        System.out.println("time = " + time);
    }

    public static void test5(Jedis jedis) {
        System.out.println("type");
        jedis.flushDB();

        jedis.set("key1","111");

        jedis.lpush("key2","111","222","333");

        String type = jedis.type("key1");
        System.out.println("type = " + type);

        type = jedis.type("key2");
        System.out.println("type = " + type);
    }

    public static void main(String[] args) {
        // 链接redis
        JedisPool  jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try (Jedis jedis = jedisPool.getResource()) {
            // redis各种命令，都对应到jedis对象的各种方法
//            String pong = jedis.ping();
//            System.out.println(pong);
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }
    }
}
